<?php
/***改变属性
 *
 */
function is_something($model, $attr)
{
    if ($model->$attr) {
        return '<span class="am-icon-check change_attr" data-attr="' . $attr . '"></span>';
    }
    return '<span class="am-icon-close change_attr" data-attr="' . $attr . '"></span>';
}

/***男女统计
 * @param $model
 * @param $attr
 * @return string
 */
function is_sex($model, $attr)
{
    if ($model->$attr) {
        return '<span class="am-icon-male change_attr"  data-attr="' . $attr . '"></span>';
    }
    return '<span class="am-icon-female change_attr"  data-attr="' . $attr . '"></span>';

}

function show_brand_products($brand)
{
    if (!$brand->products->isEmpty()) {
        return '<a class="am-badge am-badge-secondary" href="' . route('shop.product.index', ['brand_id' => $brand->id]) . '">查看商品</a>';
    }
}

function show_category_products($category)
{

    if (!$category->products->isEmpty()) {
        return '<a class="am-badge am-badge-secondary" href="' . route('shop.product.index', ['category_id' => $category->id]) . '">查看商品</a>';
    }
}

/**
 * 1=> '下单',       //待支付
 * 2=> '付款',       //待发货
 * 3=> '配货',
 * 4=> '出库',       //待收货
 * 5=> '交易成功',    //已完成
 */
function order_color($status)
{
    switch ($status) {
        case '1':
            return 'uc-order-item-pay';         //橙
            break;
        case '2':
            return 'uc-order-item-shipping';    //红
            break;
        case '3':
            return 'uc-order-item-shipping';    //红
            break;
        case '4':
            return 'uc-order-item-receiving';   //绿
            break;
        case '5':
            return 'uc-order-item-finish';      //灰
            break;
        default:
            return 'uc-order-item-finish';
    }
}

/**
 * 订单状态
 * @param $status
 * @return mixed
 */
function order_status($status)
{
    $info = config('admin.order_status');
    return $info["$status"];
}

function time_format($attr, $datetime)
{
    if ($datetime == "") {
        return "";
    }
    return date($attr, strtotime($datetime));
}

/**
 * 微信菜单, 删除空数组
 * @param $buttons
 */
function wechat_menus($request_buttons)
{
    $buttons = [];

    foreach ($request_buttons as $key => $value) {
        if ($value['name'] == "") {
            continue;
        }

        $buttons["$key"] = wechat_key_url($value);

        foreach ($value["sub_button"] as $k => $v) {
            if ($v['name'] == "") {
                continue;
            }
            $buttons["$key"]["sub_button"][] = wechat_key_url($v);
        }
    }
    return $buttons;
}

/**
 * 根据类型,返回url或者key
 * @param $value
 * @return array
 */
function wechat_key_url($value)
{
    $result = [];

    $result['type'] = $value['type'];
    $result['name'] = $value['name'];
    if ($value['type'] == "click") {
        $result['key'] = $value['value'];
    } else {
        $result['url'] = $value['value'];
    }
    return $result;
}






// 权重数值越高，被返回的概率越大

// author www.phpernote.com

function countWeight($data)
{


    $weight = 0;
    $temp = array();
    foreach ($data as $v) {
        $weight += $v['weight'];
        for ($i = 0; $i < $v['weight']; $i++) {
            $temp[] = $v;//放大数组
        }
    }
    $int = mt_rand(0, $weight - 1);//获取一个随机数
    $result = $temp[$int];
    return $result;
}
